package poketrade;

import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import poketrade.entities.User;
import poketrade.managers.AcheterCredsManager;
/**
 * Servlet implementation class AcheterCreds
 */
@WebServlet("/AcheterCreds")
public class AcheterCreds extends BaseHttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	private AcheterCredsManager mgr;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String s = request.getParameter("credit");
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		try {
			float credit = Float.valueOf(s);
			session.setAttribute("user", mgr.acheter(u.getId(),credit));
			showMessage(request, response, "Félicitation, votre compte a été crédité de "+credit+" pokédolls'");
		}
		catch (Exception e){
			showMessage(request, response, "Entrée invalide :(");
			return;
		}
	}

}
